﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using Actya.Core.Extensibility;
using Actya.Core.Infrastructure;
using Actya.Core.SiteStructure;

namespace Actya.Plugins.Hello
{
	[Export(typeof(IPlugin))]
	public class HelloPlugin : IPlugin
	{
		private const string PageTypeId = "HelloPlugin.Hello";

		public void Install(ICmsContext cmsContext)
		{
			if (cmsContext.Site == null)
			{
				throw new ArgumentException("Can't install this plugin because there is no current site.");
			}
			// Create page type
			var pageType = new PageType
							{
								Id = PageTypeId,
								Controller = "Hello",
								Action = "Index",
								DisplayName = "Hello page",
								ContentModelType = "Actya.Plugins.Hello.Model.HelloModel, Actya.Plugins.Hello"
							};
			cmsContext.Site.InstallPageType(pageType);
		}

		public void Uninstall(ICmsContext cmsContext)
		{
			if (cmsContext.Site == null)
			{
				throw new ArgumentException("Can't uninstall this plugin because there is no current site.");
			}

			// Remove page type
			cmsContext.Site.UninstallPageType(cmsContext.Site.PageTypes.Single(pt => pt.Id == PageTypeId));
		}
	}
}